package com.sam.im.heiqun.callmain;

import android.app.Activity;
import android.util.Log;

import org.json.JSONObject;
import org.webrtc.IceCandidate;
import org.webrtc.PeerConnection;
import org.webrtc.PeerConnectionFactory;
import org.webrtc.SessionDescription;

import java.net.URISyntaxException;

import io.socket.client.Ack;
import io.socket.client.IO;
import io.socket.client.Socket;
import io.socket.emitter.Emitter;

public class ChatApplication  {
	public static PeerConnectionFactory pcFactory=null;
	public static PeerConnection pc=null;
	public static Socket socket = null;
	public static Activity context=null;
	public static RtcSdpObserver observer=new RtcSdpObserver();
	{
		try {
			IO.Options opts = new IO.Options();
			opts.forceNew = false;
			opts.reconnection = true;
			//17068  1010e3d28a13881ca69c0
            //17150  1010e3d28a13881cb6ac8
			socket = IO.socket("http://120.27.145.73:9092?token=1010e3d28a13881ca69c0&device=3", opts);
			/*if(Maincallactivity.isCaller){
				socket = IO.socket("http://120.27.145.73:9092?token=1010e3d28a13881cb6ac8&device=3", opts);
			}else{
				socket = IO.socket("http://120.27.145.73:9092?token=1010e3d28a13881ca69c0&device=3", opts);
			}*/
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}
		socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
			@Override
			public void call(Object... args) {
				Log.d("debug---", "连接建立 \r\n");
			}
		}).on("echo", new Emitter.Listener() {
			@Override
			public void call(Object... args) {
				Log.i("收到服务器的聊天消息：", String.valueOf(args[0]));

				// 告诉服务器，我已经收到聊天消息的ack.
				Ack ack = (Ack) args[args.length - 1];// 最后一个参数为服务器的ack
				ack.call();
			}
		}).on("chat", new Emitter.Listener() {
			// 聊天事件
			@Override
			public void call(final Object... args) {
				Ack ack = (Ack) args[args.length - 1];
				ack.call();

				String msg = String.valueOf(args[0]);
				try {
					final JSONObject js = new JSONObject(msg);
					//处理offer
					int msgType=js.getInt("messageType");
					JSONObject contentjs=new JSONObject(js.getString("content"));
					if(msgType==49){
						Log.i("info","=======app49");
						IceCandidate candidate = new IceCandidate(
								contentjs.getString("sdpMid"),
								contentjs.getInt("sdpMLineIndex"),
								contentjs.getString("candidate")
						);
						pc.addIceCandidate(candidate);
					}else if(msgType==47||msgType==48){
						Log.i("info", "call: ==="+js.toString());
						SessionDescription sdp = new SessionDescription(
								SessionDescription.Type.fromCanonicalForm(contentjs.getString("type")),
								contentjs.getString("sdp"));
						pc.setRemoteDescription(observer,sdp);
						Log.i("info","=======app48");
						if(msgType==47){
							pc.createAnswer(observer, RtcUtil.getPcConstraints());
							//发送answer消息
							JSONObject payload = new JSONObject();
							payload.put("type", sdp.type.canonicalForm());
							payload.put("sdp", sdp.description);
							String sendSdp= payload.toString();
							Log.i("info","=======app47");
							socket.emit("chat", IMMessage.createSimpleMsg(sendSdp, 48), new Ack() {
								@Override
								public void call(Object... args) {
									System.out.println("answer sent...");
								}
							});
						}
					}

					/*
					if(context!=null){
						context.runOnUiThread(new Runnable() {
							@Override
							public void run() {
								try {
									Toast.makeText(getApplicationContext(), js.getString("content"), Toast.LENGTH_SHORT).show();
								} catch (JSONException e) {
									e.printStackTrace();
								}
							}
						});
					}
					*/

				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}).on(Socket.EVENT_DISCONNECT, new Emitter.Listener() {
			@Override
			public void call(Object... args) {
				Log.d("debug---", "连接断开");
			}
		});
		socket.connect();
	}
}
